#!/usr/bin/env Rscript
##################################
############ Preamble ############
##################################
# set language to English
Sys.setenv(LANG = "en")

# clean up
rm(list = ls())

# Set working directory: Please set your own
if (Sys.getenv("RSTUDIO") == "1") setwd("~/Dropbox/cues_bjps_replication")

# Load necessary packages
library(Rmisc)
library(tidyverse)
library(readxl)
library(stargazer)
library(kableExtra)
library(sandwich)
library(broom)
library(dotwhisker)
library(jtools)
library(scales)
library(xtable)

# raw_data <- read_excel("data/i360_dataset_Normwandel_220201.xlsx", skip = 2, col_names = cols
# read data, skip second row of headers
cols <- as.character(read_excel("data/final_survey_data.xlsx", n_max = 1, col_names = FALSE))
raw_data <- read_excel("data/final_survey_data.xlsx", skip = 2, col_names = cols)

# rename variables
raw_data <- raw_data %>%
  dplyr::rename(
    number = lfdn,
    completion_time = duration,
    disposition_code = dispcode,
    treatment = c_0031,
    cover_page = v_27,
    age = v_28,
    age_range = v_29,
    gender = v_30,
    state = v_31,
    born_in_germany = v_39,
    sign_petition_self = v_47,
    sensitive_item_agree = dupl1_v_44,
    left_right = dupl1_v_41,
    petition_appropriate_self = v_48,
    sign_petition_others = v_49,
    petition_appropriate_others = v_50,
    delete_tweet = v_51,
    voted_2021 = v_53,
    party_voted_2021 = v_54,
    party_voted_2021_other_which = v_55,
    voted_2017 = v_56,
    party_voted_2017 = v_57,
    party_voted_2017_other_which = v_58,
    household_income = v_59,
    education = v_60,
    mother_born_where = v_61,
    mother_born_other_where = v_62,
    father_born_where = v_63,
    father_born_other_where = v_64
  )

raw_data <- raw_data %>%
  mutate(treatment = replace(treatment, which(treatment == 1), "Mainstream Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 2), "Mainstream Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 3), "RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 4), "RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 5), "Mainstream and RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 6), "Mainstream and RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 7), "Mainstream Disapprove and RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 8), "Mainstream Disapprove and RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 9), "Control")) %>%
  mutate(treatment = replace(treatment, which(treatment == 10), "Control"))

# recode state names
raw_data <- raw_data %>%
  mutate(state = replace(state, which(state == 1), "Baden-Wuerttemberg")) %>%
  mutate(state = replace(state, which(state == 2), "Bayern")) %>%
  mutate(state = replace(state, which(state == 3), "Berlin")) %>%
  mutate(state = replace(state, which(state == 4), "Brandenburg")) %>%
  mutate(state = replace(state, which(state == 5), "Bremen")) %>%
  mutate(state = replace(state, which(state == 6), "Hamburg")) %>%
  mutate(state = replace(state, which(state == 7), "Hessen")) %>%
  mutate(state = replace(state, which(state == 8), "Mecklenburg-Vorpommern")) %>%
  mutate(state = replace(state, which(state == 9), "Niedersachsen")) %>%
  mutate(state = replace(state, which(state == 10), "Nordrhein-Westfalen")) %>%
  mutate(state = replace(state, which(state == 11), "Rheinland-Pfalz")) %>%
  mutate(state = replace(state, which(state == 12), "Saarland")) %>%
  mutate(state = replace(state, which(state == 13), "Sachsen")) %>%
  mutate(state = replace(state, which(state == 14), "Sachsen-Anhalt")) %>%
  mutate(state = replace(state, which(state == 15), "Schleswig-Holstein")) %>%
  mutate(state = replace(state, which(state == 16), "Thueringen"))

# recode age_ranges
raw_data <- raw_data %>%
  mutate(age_range = replace(age_range, which(age_range == 1), "18 - 29 years")) %>%
  mutate(age_range = replace(age_range, which(age_range == 2), "30 - 39 years")) %>%
  mutate(age_range = replace(age_range, which(age_range == 3), "40 - 49 years")) %>%
  mutate(age_range = replace(age_range, which(age_range == 4), "50 - 59 years")) %>%
  mutate(age_range = replace(age_range, which(age_range == 5), "60 - 90 years"))

# recode gender
raw_data <- raw_data %>%
  mutate(gender = replace(gender, which(gender == 1), "Male")) %>%
  mutate(gender = replace(gender, which(gender == 2), "Female"))

# recode sensitive item - change sensitive item response "disagree" to 0
raw_data <- raw_data %>%
  mutate(sensitive_item_agree = replace(sensitive_item_agree, which(sensitive_item_agree == 2), 0))

# recode coordination game - change "not willing to sign" from 2 to 0
raw_data <- raw_data %>%
  mutate(sign_petition_self = replace(sign_petition_self, which(sign_petition_self == 2), 0))

# recode punishment exp - change "do not delete tweet" from 2 to 0
raw_data <- raw_data %>%
  mutate(delete_tweet = replace(delete_tweet, which(delete_tweet == 2), 0))

# recode left_right 100 is 0, 99 is NA
raw_data <- raw_data %>%
  mutate(left_right = replace(left_right, which(left_right == 100), 0)) %>%
  mutate(left_right = replace(left_right, which(left_right == 99), NA))

# recode income: recode "don't know" responses to NA
raw_data <- raw_data %>%
  mutate(household_income = replace(household_income, which(household_income == 5), NA))

# reverse scale income because it is inverted (currently 1 is highest, 4 is lowest)
raw_data <- raw_data %>% mutate(household_income = 5 - household_income)

# recode no school leaving certificate to 1 as it is currently 9 and the highest in the scale, and move all other values in scale up by 1
raw_data <- raw_data %>%
  mutate(education = replace(education, which(education == 9), 0))

raw_data <- raw_data %>% mutate(education = 1 + education)

# recode party voted
raw_data <- raw_data %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 1), "CDU/CSU")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 2), "SPD")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 3), "AfD")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 4), "Green")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 5), "FDP")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 6), "Die Linke"))

# remove incompletes or inattentives
analysis_data <- raw_data %>%
  filter(disposition_code == "31" | disposition_code == "32")

# clean up sensitive item - drop -77s, merge both columns
analysis_data <- analysis_data %>%
  mutate(sensitive_item_agree = replace(sensitive_item_agree, which(sensitive_item_agree < 0), NA))

# Create dummy variable
analysis_data$MRPapprovedummy <- ifelse(analysis_data$treatment == "Mainstream Approve", 1, 0)
analysis_data$RRPapprovedummy <- ifelse(analysis_data$treatment == "RRP Approve", 1, 0)
analysis_data$MRPapproveRRPapprovedummy <- ifelse(analysis_data$treatment == "Mainstream and RRP Approve", 1, 0)
analysis_data$MRPdisapproveRRPapprovedummy <- ifelse(analysis_data$treatment == "Mainstream Disapprove and RRP Approve", 1, 0)

# subset analysis data to male only
analysis_data_male <- analysis_data %>%
  filter(gender == "Male")

# RRP only male
# rrp_only_male <- analysis_data_male %>%
#   filter(party_voted_2021 == "AfD")

# right-wing only male
right_only_male <- analysis_data_male %>%
  filter(party_voted_2021 == "CDU/CSU" | party_voted_2021 == "FDP" | party_voted_2021 == "AfD")

# left-wing only male
left_only_male <- analysis_data_male %>%
  filter(party_voted_2021 == "SPD" | party_voted_2021 == "Green" | party_voted_2021 == "Die Linke")

# subset analysis data to female only
analysis_data_female <- analysis_data %>%
  filter(gender == "Female")

# RRP only female
# rrp_only_female <- analysis_data_female %>%
#   filter(party_voted_2021 == "AfD")

# right-wing only female
right_only_female <- analysis_data_female %>%
  filter(party_voted_2021 == "CDU/CSU" | party_voted_2021 == "FDP" | party_voted_2021 == "AfD")

# left-wing only female
left_only_female <- analysis_data_female %>%
  filter(party_voted_2021 == "SPD" | party_voted_2021 == "Green" | party_voted_2021 == "Die Linke")

# standardize by subtracting mean and dividing by SD
analysis_data_male[, c(18, 19, 20, 21, 22, 23)] <- scale(analysis_data_male[, c(18, 19, 20, 21, 22, 23)])
left_only_male[, c(18, 19, 20, 21, 22, 23)] <- scale(left_only_male[, c(18, 19, 20, 21, 22, 23)])
right_only_male[, c(18, 19, 20, 21, 22, 23)] <- scale(right_only_male[, c(18, 19, 20, 21, 22, 23)])
# rrp_only_male[,c(18,19,20,21,22,23)]<-scale(rrp_only_male[,c(18,19,20,21,22,23)])

analysis_data_female[, c(18, 19, 20, 21, 22, 23)] <- scale(analysis_data_female[, c(18, 19, 20, 21, 22, 23)])
left_only_female[, c(18, 19, 20, 21, 22, 23)] <- scale(left_only_female[, c(18, 19, 20, 21, 22, 23)])
right_only_female[, c(18, 19, 20, 21, 22, 23)] <- scale(right_only_female[, c(18, 19, 20, 21, 22, 23)])
# rrp_only_female[,c(18,19,20,21,22,23)]<-scale(rrp_only_female[,c(18,19,20,21,22,23)])




# Sensitive Item - Male Only

# regression for Agreement with Sensitive Item across different samples
sensitive_item_controls_fullsample_male <- lm(sensitive_item_agree ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_male)
sensitive_item_controls_leftonly_male <- lm(sensitive_item_agree ~ MRPapprovedummy * RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_male)
sensitive_item_controls_rightonly_male <- lm(sensitive_item_agree ~ MRPapprovedummy * RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_male)
# sensitive_item_controls_rrponly_male <- lm(sensitive_item_agree ~ MRPapprovedummy *RRPapprovedummy+ MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_male)

# robust SEs - "HC1" for STATA
robust_sensitive_item_controls_fullsample_male <- summ(sensitive_item_controls_fullsample_male, robust = "HC1")
robust_sensitive_item_controls_leftonly_male <- summ(sensitive_item_controls_leftonly_male, robust = "HC1")
robust_sensitive_item_controls_rightonly_male <- summ(sensitive_item_controls_rightonly_male, robust = "HC1")
# robust_sensitive_item_controls_rrponly_male <- summ(sensitive_item_controls_rrponly_male, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_sensitive_item_controls_fullsample_male_df <- broom::tidy(robust_sensitive_item_controls_fullsample_male) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Agreement with Sensitive Item") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sensitive_item_controls_fullsample_male_df)[1] <- "model"
names(robust_sensitive_item_controls_fullsample_male_df)[7] <- "term"

robust_sensitive_item_controls_leftonly_male_df <- broom::tidy(robust_sensitive_item_controls_leftonly_male) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Agreement with Sensitive Item") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sensitive_item_controls_leftonly_male_df)[1] <- "model"
names(robust_sensitive_item_controls_leftonly_male_df)[7] <- "term"


robust_sensitive_item_controls_rightonly_male_df <- broom::tidy(robust_sensitive_item_controls_rightonly_male) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Agreement with Sensitive Item") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sensitive_item_controls_rightonly_male_df)[1] <- "model"
names(robust_sensitive_item_controls_rightonly_male_df)[7] <- "term"

# robust_sensitive_item_controls_rrponly_male_df <- broom::tidy(robust_sensitive_item_controls_rrponly_male) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Agreement with Sensitive Item") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right")  %>%
#   filter(term != "(Intercept)")

# rename for the dwplot
# names(robust_sensitive_item_controls_rrponly_male_df)[1] <- "model"
# names(robust_sensitive_item_controls_rrponly_male_df)[7] <- "term"


# Willingness to Sign Petition - Male only


# regression for Willingness to Sign Petition across different samples
sign_petition_self_controls_fullsample_male <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_male)
sign_petition_self_controls_leftonly_male <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_male)
sign_petition_self_controls_rightonly_male <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_male)
# sign_petition_self_controls_rrponly_male <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_male)

# robust SEs
robust_sign_petition_self_controls_fullsample_male <- summ(sign_petition_self_controls_fullsample_male, robust = "HC1")
robust_sign_petition_self_controls_leftonly_male <- summ(sign_petition_self_controls_leftonly_male, robust = "HC1")
robust_sign_petition_self_controls_rightonly_male <- summ(sign_petition_self_controls_rightonly_male, robust = "HC1")
# robust_sign_petition_self_controls_rrponly_male <- summ(sign_petition_self_controls_rrponly_male, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_sign_petition_self_controls_fullsample_male_df <- broom::tidy(robust_sign_petition_self_controls_fullsample_male) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Personal Willingness to Sign Petition") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_self_controls_fullsample_male_df)[1] <- "model"
names(robust_sign_petition_self_controls_fullsample_male_df)[7] <- "term"

robust_sign_petition_self_controls_leftonly_male_df <- broom::tidy(robust_sign_petition_self_controls_leftonly_male) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Personal Willingness to Sign Petition") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_self_controls_leftonly_male_df)[1] <- "model"
names(robust_sign_petition_self_controls_leftonly_male_df)[7] <- "term"


robust_sign_petition_self_controls_rightonly_male_df <- broom::tidy(robust_sign_petition_self_controls_rightonly_male) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Personal Willingness to Sign Petition") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_self_controls_rightonly_male_df)[1] <- "model"
names(robust_sign_petition_self_controls_rightonly_male_df)[7] <- "term"

# robust_sign_petition_self_controls_rrponly_male_df <- broom::tidy(robust_sign_petition_self_controls_rrponly_male) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Personal Willingness to Sign Petition") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right")%>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_sign_petition_self_controls_rrponly_male_df)[1] <- "model"
# names(robust_sign_petition_self_controls_rrponly_male_df)[7] <- "term"


# Personal Views about Appropriateness of Signing Petition - Male only

# regression for Personal Views about Appropriateness of Signing Petition across different samples
petition_appropriate_self_controls_fullsample_male <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_male)
petition_appropriate_self_controls_leftonly_male <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_male)
petition_appropriate_self_controls_rightonly_male <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_male)
# petition_appropriate_self_controls_rrponly_male <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_male)

# robust SEs
robust_petition_appropriate_self_controls_fullsample_male <- summ(petition_appropriate_self_controls_fullsample_male, robust = "HC1")
robust_petition_appropriate_self_controls_leftonly_male <- summ(petition_appropriate_self_controls_leftonly_male, robust = "HC1")
robust_petition_appropriate_self_controls_rightonly_male <- summ(petition_appropriate_self_controls_rightonly_male, robust = "HC1")
# robust_petition_appropriate_self_controls_rrponly_male <- summ(petition_appropriate_self_controls_rrponly_male, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_petition_appropriate_self_controls_fullsample_male_df <- broom::tidy(robust_petition_appropriate_self_controls_fullsample_male) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Personal Views of Appropriateness of Signing") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_self_controls_fullsample_male_df)[1] <- "model"
names(robust_petition_appropriate_self_controls_fullsample_male_df)[7] <- "term"

robust_petition_appropriate_self_controls_leftonly_male_df <- broom::tidy(robust_petition_appropriate_self_controls_leftonly_male) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Personal Views of Appropriateness of Signing") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_self_controls_leftonly_male_df)[1] <- "model"
names(robust_petition_appropriate_self_controls_leftonly_male_df)[7] <- "term"


robust_petition_appropriate_self_controls_rightonly_male_df <- broom::tidy(robust_petition_appropriate_self_controls_rightonly_male) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Personal Views of Appropriateness of Signing") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_self_controls_rightonly_male_df)[1] <- "model"
names(robust_petition_appropriate_self_controls_rightonly_male_df)[7] <- "term"

# robust_petition_appropriate_self_controls_rrponly_male_df <- broom::tidy(robust_petition_appropriate_self_controls_rrponly_male) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Personal Views of Appropriateness of Signing") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right")%>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_petition_appropriate_self_controls_rrponly_male_df)[1] <- "model"
# names(robust_petition_appropriate_self_controls_rrponly_male_df)[7] <- "term"



# Empirical Expectations - Male only

# regression for Empirical Expectations across different samples
sign_petition_others_controls_fullsample_male <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_male)
sign_petition_others_controls_leftonly_male <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_male)
sign_petition_others_controls_rightonly_male <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_male)
# sign_petition_others_controls_rrponly_male <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_male)

# robust SEs
robust_sign_petition_others_controls_fullsample_male <- summ(sign_petition_others_controls_fullsample_male, robust = "HC1")
robust_sign_petition_others_controls_leftonly_male <- summ(sign_petition_others_controls_leftonly_male, robust = "HC1")
robust_sign_petition_others_controls_rightonly_male <- summ(sign_petition_others_controls_rightonly_male, robust = "HC1")
# robust_sign_petition_others_controls_rrponly_male <- summ(sign_petition_others_controls_rrponly_male, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_sign_petition_others_controls_fullsample_male_df <- broom::tidy(robust_sign_petition_others_controls_fullsample_male) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Empirical Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_others_controls_fullsample_male_df)[1] <- "model"
names(robust_sign_petition_others_controls_fullsample_male_df)[7] <- "term"


robust_sign_petition_others_controls_leftonly_male_df <- broom::tidy(robust_sign_petition_others_controls_leftonly_male) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Empirical Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_others_controls_leftonly_male_df)[1] <- "model"
names(robust_sign_petition_others_controls_leftonly_male_df)[7] <- "term"


robust_sign_petition_others_controls_rightonly_male_df <- broom::tidy(robust_sign_petition_others_controls_rightonly_male) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Empirical Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_others_controls_rightonly_male_df)[1] <- "model"
names(robust_sign_petition_others_controls_rightonly_male_df)[7] <- "term"


# robust_sign_petition_others_controls_rrponly_male_df <- broom::tidy(robust_sign_petition_others_controls_rrponly_male) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Empirical Expectations") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right") %>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_sign_petition_others_controls_rrponly_male_df)[1] <- "model"
# names(robust_sign_petition_others_controls_rrponly_male_df)[7] <- "term"



# Normative Expectations - Male only

# regression for Normative Expectations across different samples
petition_appropriate_others_controls_fullsample_male <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_male)
petition_appropriate_others_controls_leftonly_male <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_male)
petition_appropriate_others_controls_rightonly_male <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_male)
# petition_appropriate_others_controls_rrponly_male <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_male)

# robust SEs
robust_petition_appropriate_others_controls_fullsample_male <- summ(petition_appropriate_others_controls_fullsample_male, robust = "HC1")
robust_petition_appropriate_others_controls_leftonly_male <- summ(petition_appropriate_others_controls_leftonly_male, robust = "HC1")
robust_petition_appropriate_others_controls_rightonly_male <- summ(petition_appropriate_others_controls_rightonly_male, robust = "HC1")
# robust_petition_appropriate_others_controls_rrponly_male <- summ(petition_appropriate_others_controls_rrponly_male, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_petition_appropriate_others_controls_fullsample_male_df <- broom::tidy(robust_petition_appropriate_others_controls_fullsample_male) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Normative Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_others_controls_fullsample_male_df)[1] <- "model"
names(robust_petition_appropriate_others_controls_fullsample_male_df)[7] <- "term"

robust_petition_appropriate_others_controls_leftonly_male_df <- broom::tidy(robust_petition_appropriate_others_controls_leftonly_male) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Normative Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_others_controls_leftonly_male_df)[1] <- "model"
names(robust_petition_appropriate_others_controls_leftonly_male_df)[7] <- "term"

robust_petition_appropriate_others_controls_rightonly_male_df <- broom::tidy(robust_petition_appropriate_others_controls_rightonly_male) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Normative Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_others_controls_rightonly_male_df)[1] <- "model"
names(robust_petition_appropriate_others_controls_rightonly_male_df)[7] <- "term"

# robust_petition_appropriate_others_controls_rrponly_male_df <- broom::tidy(robust_petition_appropriate_others_controls_rrponly_male) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Normative Expectations") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right")%>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_petition_appropriate_others_controls_rrponly_male_df)[1] <- "model"
# names(robust_petition_appropriate_others_controls_rrponly_male_df)[7] <- "term"



# Sanctioning - Male only

# regression for Sanctioning across different samples
delete_tweet_controls_fullsample_male <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_male)
delete_tweet_controls_leftonly_male <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_male)
delete_tweet_controls_rightonly_male <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_male)
# delete_tweet_controls_rrponly_male <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_male)

# robust SEs
robust_delete_tweet_controls_fullsample_male <- summ(delete_tweet_controls_fullsample_male, robust = "HC1")
robust_delete_tweet_controls_leftonly_male <- summ(delete_tweet_controls_leftonly_male, robust = "HC1")
robust_delete_tweet_controls_rightonly_male <- summ(delete_tweet_controls_rightonly_male, robust = "HC1")
# robust_delete_tweet_controls_rrponly_male <- summ(delete_tweet_controls_rrponly_male, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_delete_tweet_controls_fullsample_male_df <- broom::tidy(robust_delete_tweet_controls_fullsample_male) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Sanctioning") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_delete_tweet_controls_fullsample_male_df)[1] <- "model"
names(robust_delete_tweet_controls_fullsample_male_df)[7] <- "term"

robust_delete_tweet_controls_leftonly_male_df <- broom::tidy(robust_delete_tweet_controls_leftonly_male) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Sanctioning") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_delete_tweet_controls_leftonly_male_df)[1] <- "model"
names(robust_delete_tweet_controls_leftonly_male_df)[7] <- "term"


robust_delete_tweet_controls_rightonly_male_df <- broom::tidy(robust_delete_tweet_controls_rightonly_male) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Sanctioning") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_delete_tweet_controls_rightonly_male_df)[1] <- "model"
names(robust_delete_tweet_controls_rightonly_male_df)[7] <- "term"

# robust_delete_tweet_controls_rrponly_male_df <- broom::tidy(robust_delete_tweet_controls_rrponly_male) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Sanctioning") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right") %>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_delete_tweet_controls_rrponly_male_df)[1] <- "model"
# names(robust_delete_tweet_controls_rrponly_male_df)[7] <- "term"

# final joining of all the models
joined_models_controls_male <- rbind(
  robust_sign_petition_self_controls_fullsample_male_df,
  robust_sign_petition_self_controls_leftonly_male_df,
  robust_sign_petition_self_controls_rightonly_male_df,
  # robust_sign_petition_self_controls_rrponly_male_df,
  robust_sensitive_item_controls_fullsample_male_df,
  robust_sensitive_item_controls_leftonly_male_df,
  robust_sensitive_item_controls_rightonly_male_df,
  # robust_sensitive_item_controls_rrponly_male_df,
  robust_petition_appropriate_self_controls_fullsample_male_df,
  robust_petition_appropriate_self_controls_leftonly_male_df,
  robust_petition_appropriate_self_controls_rightonly_male_df,
  # robust_petition_appropriate_self_controls_rrponly_male_df,
  robust_sign_petition_others_controls_fullsample_male_df,
  robust_sign_petition_others_controls_leftonly_male_df,
  robust_sign_petition_others_controls_rightonly_male_df,
  # robust_sign_petition_others_controls_rrponly_male_df,
  robust_petition_appropriate_others_controls_fullsample_male_df,
  robust_petition_appropriate_others_controls_leftonly_male_df,
  robust_petition_appropriate_others_controls_rightonly_male_df,
  # robust_petition_appropriate_others_controls_rrponly_male_df,
  robust_delete_tweet_controls_fullsample_male_df,
  robust_delete_tweet_controls_leftonly_male_df,
  robust_delete_tweet_controls_rightonly_male_df
  # robust_delete_tweet_controls_rrponly_male_df
)


# reorder to specify sequence in facet_wrap
joined_models_controls_male$sample <- factor(joined_models_controls_male$sample, # Reordering group factor levels
  levels = c("Full Sample", "Right Wing Only", "Left Wing Only")
)

# final dwplot code

fig_e5 <- dwplot(joined_models_controls_male,
  vline = geom_vline(
    xintercept = 0,
    colour = "grey60",
    linetype = 2
  ),
  dot_args = list(aes(shape = model)),
  whisker_args = list(aes(linetype = model))
) +
  facet_wrap(~sample, nrow = 1) +
  theme(strip.text = element_text(size = 5)) +
  scale_colour_grey(
    start = .1,
    end = .1,
    # if start and end same value, use same colour for all models
    labels = c("MRP Approve and RRP Approve vs Control", "MRP Disapprove and RRP Approve vs Control", "RRP Approve vs Control", "MRP Approve vs Control")
  ) +
  labs(title = "Male Only: Treatment Conditions against Control Condition (With Controls)") +
  scale_shape_discrete(labels = c("MRP Approve and RRP Approve vs Control", "MRP Disapprove and RRP Approve vs Control", "RRP Approve vs Control", "MRP Approve vs Control")) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.text = element_text(size = rel(0.9))) +
  guides(
    shape = guide_legend("Treatment Condition", reverse = TRUE),
    colour = guide_legend("Treatment Condition", reverse = TRUE)
  ) + # Combine the legends for shape and color
  scale_y_discrete(labels = label_wrap(13))

ggsave(
  filename = "plots/fig_e5.png", plot = fig_e5,
  width = 15, height = 10
)



# Sensitive Item - Female Only

# regression for Agreement with Sensitive Item across different samples
sensitive_item_controls_fullsample_female <- lm(sensitive_item_agree ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_female)
sensitive_item_controls_leftonly_female <- lm(sensitive_item_agree ~ MRPapprovedummy * RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_female)
sensitive_item_controls_rightonly_female <- lm(sensitive_item_agree ~ MRPapprovedummy * RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_female)
# sensitive_item_controls_rrponly_female <- lm(sensitive_item_agree ~ MRPapprovedummy *RRPapprovedummy+ MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_female)

# robust SEs - "HC1" for STATA
robust_sensitive_item_controls_fullsample_female <- summ(sensitive_item_controls_fullsample_female, robust = "HC1")
robust_sensitive_item_controls_leftonly_female <- summ(sensitive_item_controls_leftonly_female, robust = "HC1")
robust_sensitive_item_controls_rightonly_female <- summ(sensitive_item_controls_rightonly_female, robust = "HC1")
# robust_sensitive_item_controls_rrponly_female <- summ(sensitive_item_controls_rrponly_female, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_sensitive_item_controls_fullsample_female_df <- broom::tidy(robust_sensitive_item_controls_fullsample_female) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Agreement with Sensitive Item") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sensitive_item_controls_fullsample_female_df)[1] <- "model"
names(robust_sensitive_item_controls_fullsample_female_df)[7] <- "term"

robust_sensitive_item_controls_leftonly_female_df <- broom::tidy(robust_sensitive_item_controls_leftonly_female) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Agreement with Sensitive Item") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sensitive_item_controls_leftonly_female_df)[1] <- "model"
names(robust_sensitive_item_controls_leftonly_female_df)[7] <- "term"


robust_sensitive_item_controls_rightonly_female_df <- broom::tidy(robust_sensitive_item_controls_rightonly_female) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Agreement with Sensitive Item") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sensitive_item_controls_rightonly_female_df)[1] <- "model"
names(robust_sensitive_item_controls_rightonly_female_df)[7] <- "term"

# robust_sensitive_item_controls_rrponly_female_df <- broom::tidy(robust_sensitive_item_controls_rrponly_female) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Agreement with Sensitive Item") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right")  %>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_sensitive_item_controls_rrponly_female_df)[1] <- "model"
# names(robust_sensitive_item_controls_rrponly_female_df)[7] <- "term"


# Willingness to Sign Petition- Female Only


# regression for Willingness to Sign Petition across different samples
sign_petition_self_controls_fullsample_female <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_female)
sign_petition_self_controls_leftonly_female <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_female)
sign_petition_self_controls_rightonly_female <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_female)
# sign_petition_self_controls_rrponly_female <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_female)

# robust SEs
robust_sign_petition_self_controls_fullsample_female <- summ(sign_petition_self_controls_fullsample_female, robust = "HC1")
robust_sign_petition_self_controls_leftonly_female <- summ(sign_petition_self_controls_leftonly_female, robust = "HC1")
robust_sign_petition_self_controls_rightonly_female <- summ(sign_petition_self_controls_rightonly_female, robust = "HC1")
# robust_sign_petition_self_controls_rrponly_female <- summ(sign_petition_self_controls_rrponly_female, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_sign_petition_self_controls_fullsample_female_df <- broom::tidy(robust_sign_petition_self_controls_fullsample_female) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Personal Willingness to Sign Petition") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_self_controls_fullsample_female_df)[1] <- "model"
names(robust_sign_petition_self_controls_fullsample_female_df)[7] <- "term"

robust_sign_petition_self_controls_leftonly_female_df <- broom::tidy(robust_sign_petition_self_controls_leftonly_female) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Personal Willingness to Sign Petition") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_self_controls_leftonly_female_df)[1] <- "model"
names(robust_sign_petition_self_controls_leftonly_female_df)[7] <- "term"


robust_sign_petition_self_controls_rightonly_female_df <- broom::tidy(robust_sign_petition_self_controls_rightonly_female) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Personal Willingness to Sign Petition") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_self_controls_rightonly_female_df)[1] <- "model"
names(robust_sign_petition_self_controls_rightonly_female_df)[7] <- "term"

# robust_sign_petition_self_controls_rrponly_female_df <- broom::tidy(robust_sign_petition_self_controls_rrponly_female) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Personal Willingness to Sign Petition") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right")%>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_sign_petition_self_controls_rrponly_female_df)[1] <- "model"
# names(robust_sign_petition_self_controls_rrponly_female_df)[7] <- "term"


# Personal Views about Appropriateness of Signing Petition - Female Only

# regression for Personal Views about Appropriateness of Signing Petition across different samples
petition_appropriate_self_controls_fullsample_female <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_female)
petition_appropriate_self_controls_leftonly_female <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_female)
petition_appropriate_self_controls_rightonly_female <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_female)
# petition_appropriate_self_controls_rrponly_female <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_female)

# robust SEs
robust_petition_appropriate_self_controls_fullsample_female <- summ(petition_appropriate_self_controls_fullsample_female, robust = "HC1")
robust_petition_appropriate_self_controls_leftonly_female <- summ(petition_appropriate_self_controls_leftonly_female, robust = "HC1")
robust_petition_appropriate_self_controls_rightonly_female <- summ(petition_appropriate_self_controls_rightonly_female, robust = "HC1")
# robust_petition_appropriate_self_controls_rrponly_female <- summ(petition_appropriate_self_controls_rrponly_female, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_petition_appropriate_self_controls_fullsample_female_df <- broom::tidy(robust_petition_appropriate_self_controls_fullsample_female) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Personal Views of Appropriateness of Signing") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_self_controls_fullsample_female_df)[1] <- "model"
names(robust_petition_appropriate_self_controls_fullsample_female_df)[7] <- "term"

robust_petition_appropriate_self_controls_leftonly_female_df <- broom::tidy(robust_petition_appropriate_self_controls_leftonly_female) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Personal Views of Appropriateness of Signing") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_self_controls_leftonly_female_df)[1] <- "model"
names(robust_petition_appropriate_self_controls_leftonly_female_df)[7] <- "term"


robust_petition_appropriate_self_controls_rightonly_female_df <- broom::tidy(robust_petition_appropriate_self_controls_rightonly_female) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Personal Views of Appropriateness of Signing") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_self_controls_rightonly_female_df)[1] <- "model"
names(robust_petition_appropriate_self_controls_rightonly_female_df)[7] <- "term"

# robust_petition_appropriate_self_controls_rrponly_female_df <- broom::tidy(robust_petition_appropriate_self_controls_rrponly_female) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Personal Views of Appropriateness of Signing") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right")%>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_petition_appropriate_self_controls_rrponly_female_df)[1] <- "model"
# names(robust_petition_appropriate_self_controls_rrponly_female_df)[7] <- "term"



# Empirical Expectations - Female Only

# regression for Empirical Expectations across different samples
sign_petition_others_controls_fullsample_female <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_female)
sign_petition_others_controls_leftonly_female <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_female)
sign_petition_others_controls_rightonly_female <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_female)
# sign_petition_others_controls_rrponly_female <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_female)

# robust SEs
robust_sign_petition_others_controls_fullsample_female <- summ(sign_petition_others_controls_fullsample_female, robust = "HC1")
robust_sign_petition_others_controls_leftonly_female <- summ(sign_petition_others_controls_leftonly_female, robust = "HC1")
robust_sign_petition_others_controls_rightonly_female <- summ(sign_petition_others_controls_rightonly_female, robust = "HC1")
# robust_sign_petition_others_controls_rrponly_female <- summ(sign_petition_others_controls_rrponly_female, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_sign_petition_others_controls_fullsample_female_df <- broom::tidy(robust_sign_petition_others_controls_fullsample_female) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Empirical Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_others_controls_fullsample_female_df)[1] <- "model"
names(robust_sign_petition_others_controls_fullsample_female_df)[7] <- "term"


robust_sign_petition_others_controls_leftonly_female_df <- broom::tidy(robust_sign_petition_others_controls_leftonly_female) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Empirical Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_others_controls_leftonly_female_df)[1] <- "model"
names(robust_sign_petition_others_controls_leftonly_female_df)[7] <- "term"


robust_sign_petition_others_controls_rightonly_female_df <- broom::tidy(robust_sign_petition_others_controls_rightonly_female) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Empirical Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_sign_petition_others_controls_rightonly_female_df)[1] <- "model"
names(robust_sign_petition_others_controls_rightonly_female_df)[7] <- "term"


# robust_sign_petition_others_controls_rrponly_female_df <- broom::tidy(robust_sign_petition_others_controls_rrponly_female) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Empirical Expectations") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right") %>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_sign_petition_others_controls_rrponly_female_df)[1] <- "model"
# names(robust_sign_petition_others_controls_rrponly_female_df)[7] <- "term"



# Normative Expectations - Female Only

# regression for Normative Expectations across different samples
petition_appropriate_others_controls_fullsample_female <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_female)
petition_appropriate_others_controls_leftonly_female <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_female)
petition_appropriate_others_controls_rightonly_female <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_female)
# petition_appropriate_others_controls_rrponly_female <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_female)

# robust SEs
robust_petition_appropriate_others_controls_fullsample_female <- summ(petition_appropriate_others_controls_fullsample_female, robust = "HC1")
robust_petition_appropriate_others_controls_leftonly_female <- summ(petition_appropriate_others_controls_leftonly_female, robust = "HC1")
robust_petition_appropriate_others_controls_rightonly_female <- summ(petition_appropriate_others_controls_rightonly_female, robust = "HC1")
# robust_petition_appropriate_others_controls_rrponly_female <- summ(petition_appropriate_others_controls_rrponly_female, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_petition_appropriate_others_controls_fullsample_female_df <- broom::tidy(robust_petition_appropriate_others_controls_fullsample_female) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Normative Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_others_controls_fullsample_female_df)[1] <- "model"
names(robust_petition_appropriate_others_controls_fullsample_female_df)[7] <- "term"

robust_petition_appropriate_others_controls_leftonly_female_df <- broom::tidy(robust_petition_appropriate_others_controls_leftonly_female) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Normative Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_others_controls_leftonly_female_df)[1] <- "model"
names(robust_petition_appropriate_others_controls_leftonly_female_df)[7] <- "term"

robust_petition_appropriate_others_controls_rightonly_female_df <- broom::tidy(robust_petition_appropriate_others_controls_rightonly_female) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Normative Expectations") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_petition_appropriate_others_controls_rightonly_female_df)[1] <- "model"
names(robust_petition_appropriate_others_controls_rightonly_female_df)[7] <- "term"

# robust_petition_appropriate_others_controls_rrponly_female_df <- broom::tidy(robust_petition_appropriate_others_controls_rrponly_female) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Normative Expectations") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right")%>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_petition_appropriate_others_controls_rrponly_female_df)[1] <- "model"
# names(robust_petition_appropriate_others_controls_rrponly_female_df)[7] <- "term"



# Sanctioning - Female Only

# regression for Sanctioning across different samples
delete_tweet_controls_fullsample_female <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_female)
delete_tweet_controls_leftonly_female <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_female)
delete_tweet_controls_rightonly_female <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_female)
# delete_tweet_controls_rrponly_female <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_female)

# robust SEs
robust_delete_tweet_controls_fullsample_female <- summ(delete_tweet_controls_fullsample_female, robust = "HC1")
robust_delete_tweet_controls_leftonly_female <- summ(delete_tweet_controls_leftonly_female, robust = "HC1")
robust_delete_tweet_controls_rightonly_female <- summ(delete_tweet_controls_rightonly_female, robust = "HC1")
# robust_delete_tweet_controls_rrponly_female <- summ(delete_tweet_controls_rrponly_female, robust = "HC1")

# convert regression outcomes to df for coefficient plot
robust_delete_tweet_controls_fullsample_female_df <- broom::tidy(robust_delete_tweet_controls_fullsample_female) %>%
  mutate(sample = "Full Sample") %>%
  mutate(measure = "Sanctioning") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_delete_tweet_controls_fullsample_female_df)[1] <- "model"
names(robust_delete_tweet_controls_fullsample_female_df)[7] <- "term"

robust_delete_tweet_controls_leftonly_female_df <- broom::tidy(robust_delete_tweet_controls_leftonly_female) %>%
  mutate(sample = "Left Wing Only") %>%
  mutate(measure = "Sanctioning") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_delete_tweet_controls_leftonly_female_df)[1] <- "model"
names(robust_delete_tweet_controls_leftonly_female_df)[7] <- "term"


robust_delete_tweet_controls_rightonly_female_df <- broom::tidy(robust_delete_tweet_controls_rightonly_female) %>%
  mutate(sample = "Right Wing Only") %>%
  mutate(measure = "Sanctioning") %>%
  filter(term != "age") %>%
  filter(term != "household_income") %>%
  filter(term != "education") %>%
  filter(term != "left_right") %>%
  filter(term != "(Intercept)")

# rename for the dwplot
names(robust_delete_tweet_controls_rightonly_female_df)[1] <- "model"
names(robust_delete_tweet_controls_rightonly_female_df)[7] <- "term"

# robust_delete_tweet_controls_rrponly_female_df <- broom::tidy(robust_delete_tweet_controls_rrponly_female) %>%
#   mutate(sample = "Radical Right Only") %>%
#   mutate(measure = "Sanctioning") %>%
#   filter(term != "age") %>%
#   filter(term != "household_income")  %>%
#   filter(term != "education")  %>%
#   filter(term != "left_right") %>%
#   filter(term != "(Intercept)")
#
# #rename for the dwplot
# names(robust_delete_tweet_controls_rrponly_female_df)[1] <- "model"
# names(robust_delete_tweet_controls_rrponly_female_df)[7] <- "term"

# final joining of all the models
joined_models_controls_female <- rbind(
  robust_sign_petition_self_controls_fullsample_female_df,
  robust_sign_petition_self_controls_leftonly_female_df,
  robust_sign_petition_self_controls_rightonly_female_df,
  # robust_sign_petition_self_controls_rrponly_female_df,
  robust_sensitive_item_controls_fullsample_female_df,
  robust_sensitive_item_controls_leftonly_female_df,
  robust_sensitive_item_controls_rightonly_female_df,
  # robust_sensitive_item_controls_rrponly_female_df,
  robust_petition_appropriate_self_controls_fullsample_female_df,
  robust_petition_appropriate_self_controls_leftonly_female_df,
  robust_petition_appropriate_self_controls_rightonly_female_df,
  # robust_petition_appropriate_self_controls_rrponly_female_df,
  robust_sign_petition_others_controls_fullsample_female_df,
  robust_sign_petition_others_controls_leftonly_female_df,
  robust_sign_petition_others_controls_rightonly_female_df,
  # robust_sign_petition_others_controls_rrponly_female_df,
  robust_petition_appropriate_others_controls_fullsample_female_df,
  robust_petition_appropriate_others_controls_leftonly_female_df,
  robust_petition_appropriate_others_controls_rightonly_female_df,
  # robust_petition_appropriate_others_controls_rrponly_female_df,
  robust_delete_tweet_controls_fullsample_female_df,
  robust_delete_tweet_controls_leftonly_female_df,
  robust_delete_tweet_controls_rightonly_female_df
  # robust_delete_tweet_controls_rrponly_female_df
)


# reorder to specify sequence in facet_wrap
joined_models_controls_female$sample <- factor(joined_models_controls_female$sample, # Reordering group factor levels
  levels = c("Full Sample", "Right Wing Only", "Left Wing Only", "Radical Right Only")
)

# final dwplot code

fig_e4 <- dwplot(joined_models_controls_female,
  vline = geom_vline(
    xintercept = 0,
    colour = "grey60",
    linetype = 2
  ),
  dot_args = list(aes(shape = model)),
  whisker_args = list(aes(linetype = model))
) +
  facet_wrap(~sample, nrow = 1) +
  theme(strip.text = element_text(size = 5)) +
  scale_colour_grey(
    start = .1,
    end = .1,
    # if start and end same value, use same colour for all models
    labels = c("MRP Approve and RRP Approve vs Control", "MRP Disapprove and RRP Approve vs Control", "RRP Approve vs Control", "MRP Approve vs Control")
  ) +
  labs(title = "Female Only: Treatment Conditions against Control Condition (With Controls)") +
  scale_shape_discrete(labels = c("MRP Approve and RRP Approve vs Control", "MRP Disapprove and RRP Approve vs Control", "RRP Approve vs Control", "MRP Approve vs Control")) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.text = element_text(size = rel(0.9))) +
  guides(
    shape = guide_legend("Treatment Condition", reverse = TRUE),
    colour = guide_legend("Treatment Condition", reverse = TRUE)
  ) + # Combine the legends for shape and color
  scale_y_discrete(labels = label_wrap(13))


ggsave(
  filename = "plots/fig_e4.png", plot = fig_e4,
  width = 15, height = 10
)
